package com.camel.iamhi.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;

@Controller
public class PageController {

    @GetMapping
    public String index(@RequestHeader(value = "X-Requested-With", required = false) String requestedWith,
                        Model model) {
        model.addAttribute("pageTitle", "首页");
        // 如果是 Ajax 请求（局部刷新），仅返回主体片段
        if ("XMLHttpRequest".equals(requestedWith)) {
            return "home/index :: content"; // 仅返回首页主体片段
        }
        // 否则返回完整页面（包含布局）
        model.addAttribute("targetFragment", "home/index :: content");
        return "full";
    }

    // 首页
    @GetMapping("/home")
    public String home(
            @RequestHeader(value = "X-Requested-With", required = false) String requestedWith,
            Model model) {
        model.addAttribute("pageTitle", "首页");
        // 如果是 Ajax 请求（局部刷新），仅返回主体片段
        if ("XMLHttpRequest".equals(requestedWith)) {
            return "home :: content"; // 仅返回首页主体片段
        }
        // 否则返回完整页面（包含布局）
        return "home-full";
    }

    // 关于页（逻辑同上）
    @GetMapping("/about")
    public String about(
            @RequestHeader(value = "X-Requested-With", required = false) String requestedWith,
            Model model) {
        model.addAttribute("pageTitle", "关于我们");
        if ("XMLHttpRequest".equals(requestedWith)) {
            return "about :: content";
        }
        return "about-full";
    }
}
